package org.bspv.pharma.manager.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bspv.pharma.dao.DAO;
import org.bspv.pharma.manager.Manager;

public abstract class AbstractManagerImpl<T, ID extends Serializable>
		implements Manager<T, ID> {

	/**
	 * Log variable for all child classes. Uses LogFactory.getLog(getClass())
	 * from Commons Logging
	 */
	protected final Log log = LogFactory.getLog(getClass());

	/**
	 * GenericDao instance, set by constructor of this class
	 */
	protected DAO<T, ID> dao;

	/**
	 * Public constructor for creating a new ManagerImpl.
	 * 
	 * @param dao
	 *            the dao to use for persistence
	 */
	public AbstractManagerImpl(final DAO<T, ID> dao) {
		this.dao = dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bspv.pharma.service.basic.Manager#exists(java.io.Serializable)
	 */
	public boolean exists(ID id) {
		return dao.findById(id) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bspv.pharma.service.basic.Manager#get(java.io.Serializable)
	 */
	public T findById(ID id) {
		return dao.findById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bspv.pharma.service.basic.Manager#getAll()
	 */
	public List<T> findAll() {
		return dao.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bspv.pharma.service.basic.Manager#remove(java.io.Serializable)
	 */
	public void delete(T e) {
		dao.delete(e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bspv.pharma.service.basic.Manager#save(java.lang.Object)
	 */
	public T save(T element) {
		return dao.merge(element);
	}

}
